package com.wrial.kind.search;
/*
 * @Author  Wrial
 * @Date Created in 21:02 2020/8/16
 * @Description 找第K大的数
 */

import java.util.PriorityQueue;

public class FindKthLargest {

    /**
     * 构建最大堆，出k-1个，堆顶就是第k大个
     */
    public int findKthLargest(int[] nums, int k) {
        //最大堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(nums.length, (a, b) -> b - a);
        for (int num : nums) {
            priorityQueue.add(num);
        }
        for (int i = 1; i < k; i++) {
            priorityQueue.poll();
        }
        return priorityQueue.peek();

    }

}
